{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "gpuType": "T4",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/DrewThomasson/ebook2audiobook/blob/v25/Notebooks/finetune/xtts/colab_xtts_finetune_webui.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Welcome to the *xtts*-finetune-webui gradio gui!\n",
        "\n",
        "This webui is a slightly modified copy of the official webui for finetune xtts.\n",
        "\n",
        "If you are looking for an option for normal XTTS use look here https://github.com/daswer123/xtts-webui"
      ],
      "metadata": {
        "id": "OVjEG_yGoC2W"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "44HpAIVRfJve",
        "collapsed": true,
        "cellView": "form"
      },
      "outputs": [],
      "source": [
        "# @title 🛠️ Install requirments\n",
        "#!DEBIAN_FRONTEND=noninteractive\n",
        "!sudo apt-get update # && sudo apt-get -y upgrade\n",
        "!sudo apt-get -y install libegl1\n",
        "!sudo apt-get -y install libopengl0\n",
        "!sudo apt-get -y install libxcb-cursor0\n",
        "!pip install -r https://raw.githubusercontent.com/daswer123/xtts-finetune-webui/main/requirements.txt\n",
        "!pip install gradio==4.44.1\n",
        "!pip install fastapi==0.103.1\n",
        "!pip install pydantic==2.3.0\n",
        "\n",
        "#More requirements\n",
        "!nvcc --version #check cuda version if things break\n",
        "!pip uninstall torch torchvision torchaudio -y\n",
        "!pip install torch==2.1.2 torchvision==0.16.2 torchaudio==2.1.2 --index-url https://download.pytorch.org/whl/cu121\n"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# @title 🚀 Run interface\n",
        "%cd /content/\n",
        "!git clone https://github.com/DrewThomasson/xtts-finetune-webui.git\n",
        "%cd /content/xtts-finetune-webui\n",
        "!python xtts_demo.py --share"
      ],
      "metadata": {
        "id": "62Da1Q5AgN3W",
        "cellView": "form"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import shutil\n",
        "import requests\n",
        "import os\n",
        "from tqdm import tqdm  # Progress bar library\n",
        "\n",
        "# Define the paths\n",
        "finetune_dir = '/content/xtts-finetune-webui/finetune_models/ready'  # @param {type:\"string\"}\n",
        "dataset_dir = '/content/xtts-finetune-webui/finetune_models/dataset'  # @param {type:\"string\"}\n",
        "\n",
        "# Create a temporary directory to collect both folders before zipping\n",
        "temp_dir = \"/content/temp_finetune_dataset\"\n",
        "os.makedirs(temp_dir, exist_ok=True)\n",
        "\n",
        "# Copy both directories into the temporary directory with a progress bar\n",
        "def copy_with_progress(src, dst):\n",
        "    total_files = sum(len(files) for _, _, files in os.walk(src))\n",
        "    with tqdm(total=total_files, desc=f\"Copying {os.path.basename(src)}\") as pbar:\n",
        "        for root, _, files in os.walk(src):\n",
        "            rel_path = os.path.relpath(root, src)\n",
        "            target_path = os.path.join(dst, rel_path)\n",
        "            os.makedirs(target_path, exist_ok=True)\n",
        "            for file in files:\n",
        "                shutil.copy(os.path.join(root, file), target_path)\n",
        "                pbar.update(1)\n",
        "\n",
        "copy_with_progress(finetune_dir, os.path.join(temp_dir, \"ready\"))\n",
        "copy_with_progress(dataset_dir, os.path.join(temp_dir, \"dataset\"))\n",
        "\n",
        "# Create a zip file of the combined directories with progress\n",
        "zip_filename = \"finetune_and_dataset.zip\"\n",
        "with tqdm(total=100, desc=\"Zipping files\") as pbar:\n",
        "    shutil.make_archive(\"finetune_and_dataset\", 'zip', root_dir=temp_dir)\n",
        "    pbar.update(100)\n",
        "\n",
        "# Define a function to stream the upload with a progress bar\n",
        "def upload_with_progress(file_path, url):\n",
        "    file_size = os.path.getsize(file_path)\n",
        "    with open(file_path, 'rb') as f, tqdm(\n",
        "        total=file_size, unit='B', unit_scale=True, desc=\"Uploading\"\n",
        "    ) as progress:\n",
        "        response = requests.post(\n",
        "            url,\n",
        "            files={\"file\": (file_path, f)},\n",
        "            stream=True,\n",
        "            headers={\"Connection\": \"keep-alive\"},\n",
        "        )\n",
        "        # Update the progress bar as chunks are sent\n",
        "        for chunk in response.iter_content(chunk_size=4096):\n",
        "            if chunk:\n",
        "                progress.update(len(chunk))\n",
        "    return response\n",
        "\n",
        "# Upload the zip file to file.io with a progress bar\n",
        "response = upload_with_progress(zip_filename, \"https://file.io/?expires=1d\")\n",
        "\n",
        "# Parse the response and display the download link\n",
        "if response.status_code == 200:\n",
        "    download_link = response.json().get('link', 'Error: No link found.')\n",
        "    print(f\"Your file is ready: {download_link}\")\n",
        "else:\n",
        "    print(f\"Failed to upload: {response.status_code} - {response.text}\")\n"
      ],
      "metadata": {
        "id": "MYBWgKevr6S3",
        "cellView": "form"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}